from kisp import *
import random
import sys
import os
import commands
import re

class Ping(KISP):
	Type = "Sensor"
	Name = "Ping"
	Description = "Pings hostname and returns time taken"
	Arguments = ["hostname"]
	Units = "ms"

	
	def Start(self,arguments):

		ping_out = commands.getstatusoutput("ping -c 3 " + arguments[0])
		if ping_out[0] == 0 or ping_out[0] == 256:
			packet_loss = re.search('(?<=, )\d+(?=%)',ping_out[1]).group(0)
			if packet_loss == "0":
				return [0, packet_loss,ping_out[1]]
			else:
				return [1, packet_loss,ping_out[1]]
		else:
			return [-1,0,"Plugin Failed:" + str(ping_out)]
